package com.winsdom.dto;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
@Data
public class OrganizationMegAddDTO {
    @Schema(description = "用户分类 ID")
    private Long userCategoryId;
    @Schema(description = "组织ID")
    private Long categoryId;
    @Schema(description = "组织名称(企业名称)",required = true,example = "XXX组织")
    private String organizationName;
    @Schema(description = "上级组织id",required = true,example = "5")
    private Long parentId;
    @Schema(description = "统一社会信用码")
    private String societyCode;
    @Schema(description = "开户行")
    private String openingBank;
    @Schema(description = "银行账号")
    private String bankAccount;
    @Schema(description = "注册区域，例如：[\"浙江省\", \"杭州市\", \"西湖区\"]")
    @JsonDeserialize(using = AreaDeserializer.class)
    private String area;
    @Schema(description = "注册区域码，例如：[\"11\", \"1101\", \"110105\"]")
    @JsonDeserialize(using = AreaDeserializer.class)
    private String areaCode;
    @Schema(description = "详情地址", required = false)
    private String address;
    @Schema(description = "电话，例如：[\"12345678911\", \"12345678911\", \"12345678911\"]")
    @JsonDeserialize(using = AreaDeserializer.class)
    private String phone;

    @Schema(description = "是否为开票组织（0=否，1=是）")
    private Integer status;

    private static class AreaDeserializer extends JsonDeserializer<String> {
        @Override
        public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            // 获取前端传递的数组
            List<String> areaList = jsonParser.readValueAs(List.class);
            if (areaList == null || areaList.isEmpty()) {
                return Collections.emptyList().toString();
            }
            // 手动构造带双引号的字符串
            StringBuilder sb = new StringBuilder("[");
            for (int i = 0; i < areaList.size(); i++) {
                sb.append("\"").append(areaList.get(i)).append("\"");
                if (i != areaList.size() - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            return sb.toString();
        }
    }
}
